#include<bits/stdc++.h>
using namespace std;

struct node {
	int a, b, idx;
} a[50005];

struct pq {
	int idx, b;
	bool operator < (const pq &x) const {
		return b > x.b;
	};
};

int idx[50005];

int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i].a >> a[i].b;
		a[i].idx = i;
	}
	sort(a + 1, a + n + 1, [](node x, node y) {
		if (x.a != y.a)
			return x.a < y.a;
		else
			return x.b < y.b;
	});

	int ans = 0, num = 1;
	priority_queue<pq> q;

	for (int i = 1; i <= n; i++) {
		if (q.empty()) {
			q.push({num++, a[i].b});
			idx[a[i].idx] = num - 1;
			ans++;
		} else {
			pq t = q.top();
			if (t.b < a[i].a) {
				q.pop();
				q.push({t.idx, a[i].b});
				idx[a[i].idx] = t.idx;
			} else {
				q.push({num++, a[i].b});
				idx[a[i].idx] = num - 1;
				ans++;
			}
		}
	}
	cout << ans << endl;
	for (int i = 1; i <= n; i++) {
		cout << idx[i] << endl;
	}
	return 0;
}
